﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using Zeta.Bot;
using Zeta.Common.Plugins;
using Zeta.Game;
using Zeta.Game.Internals.Actors;
using Zeta.Game.Internals.SNO;

namespace LocationTracker
{

    public class Plugin : IPlugin
    {

        #region Plugin Definition

        public Version Version { get { return Constants.VERSION; } }
        public string Author { get { return Constants.AUTHOR; } }
        public string Description { get { return Constants.DESCRIPTION; } }
        public string Name { get { return Constants.NAME; } }
        public bool Equals(IPlugin other) { return (other.Name == Name) && (other.Version == Version); }
        public Window DisplayWindow { get { return null; } }

        #endregion
        
        #region DB Events

        public void OnDisabled()
        {
            BotMain.OnStart -= BotStop;
            BotMain.OnStop -= BotStart;

            GameEvents.OnGameChanged -= GameChanged;
            GameEvents.OnWorldChanged -= WorldChanged;
            GameEvents.OnGameJoined -= GameJoined;
            GameEvents.OnGameLeft -= GameLeft;

            TabUI.RemoveTab();
        }

        public void OnEnabled()
        {
            BotMain.OnStart += BotStop;
            BotMain.OnStop += BotStart;

            GameEvents.OnGameChanged += GameChanged;
            GameEvents.OnWorldChanged += WorldChanged;
            GameEvents.OnGameJoined += GameJoined;
            GameEvents.OnGameLeft += GameLeft;

            TabUI.InstallTab();

            Logger.Log("ENABLED: Location Tracker " + Version + " now in action!");
        }


        public void OnInitialize()
        {

        }

        public void OnShutdown()
        {
            Cache.Reset();   
        }

        public void OnPulse()
        {
            Pulse();
        }

        #endregion

        #region Local Variables

        private static Stopwatch timer = new Stopwatch();        
        private static Stopwatch dumpTimer = new Stopwatch();

        #endregion


        public void Pulse()
        {
            try
            {
                if (!timer.IsRunning)
                {
                    timer.Start();                    
                    return;
                }

                if (!dumpTimer.IsRunning)
                {
                    dumpTimer.Start();
                    return;
                }
                
                // antispam
                if (timer.ElapsedMilliseconds < 2000)
                {
                    return;
                }

                // Ignore if we're in a bad state
                if (ZetaDia.Me == null || !ZetaDia.Me.IsValid || !ZetaDia.Service.IsValid || !ZetaDia.IsInGame || ZetaDia.IsLoadingWorld || ZetaDia.Me.IsDead || !ZetaDia.WorldInfo.IsValid || !ZetaDia.ActInfo.IsValid)
                {
                    return;
                }

                Cache.Update();

                // Dump to Files every 60 seconds
                if (dumpTimer.ElapsedMilliseconds > 60000)
                {
                    Cache.DumpToFiles();
                    dumpTimer.Reset();                    
                }

                timer.Reset(); 

            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
            }
        }

        private void GameChanged(object src, EventArgs mea)
        {
            Cache.ResetLevel();
        }

        private void WorldChanged(object src, EventArgs mea)
        {
            Cache.ResetLevel();        
        }

        private void GameJoined(object src, EventArgs mea)
        {
            //Cache.Reset();     
        }

        private void GameLeft(object src, EventArgs mea)
        {
            Cache.DumpToFiles();        
        }

        private void BotStop(IBot bot)
        {
            Cache.DumpToFiles();        
        }

        private void BotStart(IBot bot)
        {
            //Cache.Reset();      
        }

    }


}
